package com.zc.im.service.im.group.impl;

import com.zc.im.mapper.im.group.ZcImGroupMapper;
import com.zc.im.model.im.group.ZcImGroup;
import com.zc.im.model.im.group.ZcImGroupExample;
import com.zc.im.model.im.group.ZcImGroupKey;
import com.zc.im.service.im.group.ImGroupService;
import com.zc.im.util.ImGolbalTypeContext;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 群成员表
 * Created by wangcc on 2017/7/8.
 */
@Service
public class ImGroupServiceImpl implements ImGroupService {

    @Autowired
    private ZcImGroupMapper groupMapper;
    private static Logger logger = Logger.getLogger(ImGroupServiceImpl.class);


    /**
     * 添加群
     * @param zcImGroupModel
     * @return
     */
    @Override
    public Integer insertGroup(ZcImGroup zcImGroupModel) {
        Integer groupId = null;
        if (zcImGroupModel == null) {
            logger.info("insert group is null ");
            return groupId;
        }
        zcImGroupModel.setCreatedTime(new Date());
        zcImGroupModel.setStatus(ImGolbalTypeContext.STATUS_VALID);
        try {
            groupId = groupMapper.insertSelective(zcImGroupModel);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return groupId;
    }

    /**
     *删除群
     * @param id
     * @return
     */
    @Override
    public Integer deleteGroup(Integer id) {
        Integer groupId = null;
        if (id == null) {
            logger.info("delete group is null ");
            return groupId;
        }
        try {
            ZcImGroupKey groupKey = new ZcImGroupKey();
            groupKey.setId(id);
            groupId = groupMapper.deleteByPrimaryKey(groupKey);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return groupId;
    }


    /**
     * 更新群
     * @param zcImGroup
     * @return
     */
    @Override
    public Integer updateGroup(ZcImGroup zcImGroup) {
        Integer groupId = null;
        if (zcImGroup == null) {
            logger.info("update group is null");
            return groupId;
        }
        zcImGroup.setUpdatedTime(new Date());
        try {
            ZcImGroupExample groupExample = new ZcImGroupExample();
            ZcImGroupExample.Criteria criteria = groupExample.createCriteria();
            criteria.andIdEqualTo(zcImGroup.getId());

            groupId = groupMapper.updateByExampleSelective(zcImGroup, groupExample);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return groupId;
    }

    @Override
    public ZcImGroup queryAllGroup(ZcImGroup zcImGroupModel) {
        ZcImGroup zcImGroup = null;
        if (zcImGroupModel == null) {
            logger.info("query group is null");
            return zcImGroup;
        }
        try {
            ZcImGroupExample groupExample = new ZcImGroupExample();
            groupExample.createCriteria().andIdEqualTo(zcImGroupModel.getId());
            List<ZcImGroup> zcImGroups = groupMapper.selectByExample(groupExample);
            if(zcImGroups == null || zcImGroups.size() == 0) {
               return  zcImGroup;
            }
            zcImGroup = zcImGroups.get(0);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return zcImGroup;
    }

}
